//

#import "NT_WaitDoViewController.h"
#import "NT_MenuView.h"
#import "NT_CreatWaitView.h"
#import "NT_WaitTableViewCell.h"
#import "NT_NoteDetaiViewController.h"

@interface NT_WaitDoViewController ()<UITableViewDelegate, UITableViewDataSource, NT_WaitTableViewDelete,NT_FootDeleteViewDelegate,NT_NoteDetaiViewDelegate>
{
    NSArray *_array;
}
/** */
@property (nonatomic, strong) UITableView *waitTableView;
/** */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** */
@property (nonatomic, strong) NSMutableArray *twoArray;
/** */
@property (nonatomic, strong) NT_CoreDataBase *data;
/** yes = bianji*/
@property (nonatomic, assign) BOOL  isEdit;
/** */
@property (nonatomic, strong) NT_FootDeleteView *deleView;
/** */
@property (nonatomic, strong) NT_MenuView *menuView;
/** */
@property (nonatomic, assign) BOOL  isshow;
@end

@implementation NT_WaitDoViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _data = [[NT_CoreDataBase alloc]initWithCoreData:@"WaitData" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) { }];
    [self getWaitDataaaaaaa];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setnavBatTitle:@"Wait"];
    _array = @[@"Delete",@"Add"];
    
    //
    __weak typeof(self) weakSelf = self;
    [self nt_setupLeftAndRightwrurSetting:@"Add" block:^{
        //        [weakSelf waitMenu];
        NT_CreatWaitView *vc = [[NT_CreatWaitView alloc]init];
        [vc showWaitView];//TODO:新增代办
        vc.reloadWaitBlock = ^{
            [weakSelf getWaitDataaaaaaa];
        };
    }];
    
    [self.view addSubview:self.waitTableView];
    
}

//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return _dataArray.count;
    }else
        return _twoArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    NT_WaitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[NT_WaitTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.section == 0) {
        cell.model = _dataArray[indexPath.row];
    }else{
        cell.model = _twoArray[indexPath.row];
    }
    if (_isEdit) {
        cell.deleteBtn.hidden = NO;
    }else
        cell.deleteBtn.hidden = YES;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRelativeHeight(50);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]init];
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kRelativeHeight(6))];
    label1.backgroundColor = RGB(245, 245, 245);
    [view addSubview:label1];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(kRelativeWidth(15), kRelativeHeight(16), kRelativeWidth(200), kRelativeHeight(20))];
    label.textColor = [UIColor blackColor];
    [view addSubview:label];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame)+kRelativeHeight(13), kMainScreenWidth, 1.5)];
    label2.backgroundColor = RGB(245, 245, 245);
    [view addSubview:label2];
    if (section == 0) {
        label.text = @"Wait";
    }else{
        label.text = @"Finish";
    }
    
    return view;
}
//*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (indexPath.section == 0) {
            WaitData *model = _dataArray[indexPath.row];
            [self.data ntDeleteEntity:nil key:[NSString stringWithFormat:@"id== %@",model.id] success:^{ [self getWaitDataaaaaaa];} fail:^(NSError * _Nullable error) {}];
        }else {
            WaitData *model = _twoArray[indexPath.row];
            [self.data ntDeleteEntity:nil key:[NSString stringWithFormat:@"id== %@",model.id] success:^{ [self getWaitDataaaaaaa];} fail:^(NSError * _Nullable error) {}];
        }
    }
}

// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
//*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WaitData *model = _dataArray[indexPath.row];
    if (!_isEdit) {
        NT_NoteDetaiViewController *vc = [[NT_NoteDetaiViewController alloc]init];
        vc.dateStr = model.date;
        vc.titleStr = model.title;
        vc.Id = model.id;
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
        if (indexPath.section == 0) {
            vc.noShow = YES;
        }
    }
}
/*
 //MARK:下啦
 - (void)waitMenu {
 _menuView = [[NT_MenuView alloc]init];
 if (_isEdit) {
 _menuView.dataArray = @[@"Exit"];
 }else
 _menuView.dataArray = _array;
 [_menuView showView];
 //
 __weak typeof(self) weakSelf = self;
 [_menuView setIndexBlock:^(NSInteger index) {
 if (index == 1) {
 if (weakSelf.isEdit) {
 return ;
 }
 NT_CreatWaitView *vc = [[NT_CreatWaitView alloc]init];
 [vc showWaitView];//TODO:新增代办
 vc.reloadWaitBlock = ^{
 [weakSelf getWaitDataaaaaaa];
 };
 
 }else{
 
 weakSelf.isEdit =! weakSelf.isEdit;
 [weakSelf.waitTableView reloadData];
 
 if (weakSelf.isEdit) {
 PostNotifition(@"Hidden");//TODO:底部view
 weakSelf.deleView = [[NT_FootDeleteView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(weakSelf.waitTableView.frame), kMainScreenWidth, UI_TAB_BAR_HEIGHT)];
 weakSelf.deleView.delegate = weakSelf;
 [weakSelf.view addSubview:weakSelf.deleView];
 
 }else{
 PostNotifition(@"Appear");
 [weakSelf.deleView removeFromSuperview];
 }
 }
 }];
 }
 //MARK:cell选中
 - (void)sendCell:(NT_WaitTableViewCell *)cell {
 if (cell.model.status == 1) {
 cell.model.status = 0;
 }else{
 cell.model.status = 1;
 }
 [_waitTableView reloadData];
 }
 //MARK:shanchu
 - (void)sendDeleteData {
 
 [self.data ntDeleteEntity:nil key:[NSString stringWithFormat:@"status== %d",1] success:^{ [self getWaitDataaaaaaa];} fail:^(NSError * _Nullable error) {}];
 }
 */
//MARK:改已完成
- (void)sendDataModel:(NSString *)Id {
    NSMutableDictionary *dics = [NSMutableDictionary new];
    [dics setObject:[NSNumber numberWithInteger:1] forKey:@"type"];
    
    [_data ntUpdateEntity:dics key:[NSString stringWithFormat:@"id=%@",Id] success:^{} fail:^(NSError * _Nullable error) {}];
    
}
//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)waitTableView {
    if (!_waitTableView) {
        _waitTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight-UI_NAV_BAR_HEIGHT-UI_TAB_BAR_HEIGHT) style:UITableViewStylePlain];
        _waitTableView.dataSource = self;
        _waitTableView.delegate   = self;
        _waitTableView.tableFooterView = [UIView new];
        _waitTableView.rowHeight  = kRelativeHeight(50);
    }
    return _waitTableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}
- (NSMutableArray *)twoArray {
    if (!_twoArray) {
        _twoArray = [NSMutableArray new];
    }
    return _twoArray;
}
//MARK:获取数据
- (void)getWaitDataaaaaaa{
    //
    __weak typeof(self) weakSelf = self;
    [_data ntReadEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        if (!results) return ;
        //
        [self.twoArray removeAllObjects];
        [self.dataArray removeAllObjects];
        for (WaitData *model in results) {
            NSLog(@"%ld--%@",(long)model.type,model.id);
            if (model.type == 1) {
                [self.twoArray addObject:model];
            }else
                [self.dataArray addObject:model];
        }
        
        [weakSelf.waitTableView reloadData];
        if (weakSelf.dataArray.count <= 0 && weakSelf.twoArray.count <= 0) {
            
            NT_CreatWaitView *vc = [[NT_CreatWaitView alloc]init];
            [vc showWaitView];//
            vc.reloadWaitBlock = ^{
                [weakSelf getWaitDataaaaaaa];
            };
        }
        
    } fail:^(NSError * _Nullable error) {
    }];
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (BOOL)nt_isShowLeft {
    return NO;
}

@end
